﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using JustMVCTest.Models;


namespace JustMVCTest.Controllers
{ 
    /*
    public class ExcelController : Controller
    {
        private PODBContext db = new PODBContext();

        //
        // GET: /Excel/

        public ActionResult Index(string searchPONum, string searchItemNum, string Fromdatepicker, string Todatepicker, string SearchStatus, string searchVendor)
        {
            try
            {
                Application app = new Application();
                app.Visible = false;
                
                Workbook workbook = app.Workbooks.Add(1);
                Worksheet worksheet = (Worksheet)workbook.Sheets[1];
                //Add Table

                var pos = from m in db.POs
                          select m;

                if (!string.IsNullOrEmpty(searchPONum))
                {
                    try
                    {
                        var x = Convert.ToInt64 (searchPONum);
                        pos = pos.Where(s => s.PONumber == x);
                    }
                    catch
                    {
                        ModelState.AddModelError("searchPONum", "PO Number Input Error");
                    }
                }

                if (!string.IsNullOrEmpty(searchItemNum))
                {
                    try
                    {
                        var y = Convert.ToInt32(searchItemNum);
                        pos = pos.Where(s => s.Item == y);
                    }
                    catch
                    {
                        ModelState.AddModelError("searchItemNum", "Item Number Input Error");
                    }
                }

                if (!string.IsNullOrEmpty(Fromdatepicker))
                {
                    try
                    {
                        DateTime z = Convert.ToDateTime(Fromdatepicker);
                        pos = pos.Where(s => (DateTime.Compare(s.DeliveryDate, z) >= 0));
                    }
                    catch
                    {
                        ModelState.AddModelError("Fromdatepicker", "From Date Input Error");
                    }
                }

                if (!string.IsNullOrEmpty(Todatepicker))
                {
                    try
                    {
                        DateTime q = Convert.ToDateTime(Todatepicker);
                        pos = pos.Where(s => (DateTime.Compare(s.DeliveryDate, q) <= 0));
                    }
                    catch
                    {
                        ModelState.AddModelError("Todatepicker", "To Date Input Error");
                    }
                }

                if ((string.Compare(SearchStatus, "3") != 0) && (!string.IsNullOrEmpty(SearchStatus)))
                {
                    var r = Convert.ToInt32(SearchStatus);
                    pos = pos.Where(s => s.Status == r);
                }

                if (!string.IsNullOrEmpty(searchVendor))
                {
                    pos = pos.Where(s => (string.Compare(searchVendor, s.VendorName) == 0));
                }


                //pos = DataTable(pos);

                PO[] po_arr = pos.ToArray();

                worksheet.Cells[1, 3] = "PO_Number";
                worksheet.Cells[1, 2] = "Item";
                worksheet.Cells[1, 1] = "DeliveryDate";

                int j = 2;
                int count1 = pos.Count();
                while (j < 2 + count1)
                {
                    worksheet.Cells[j, 3] = po_arr[j-2].PONumber;
                    worksheet.Cells[j, 2] = po_arr[j-2].Item;
                    worksheet.Cells[j, 1] = po_arr[j - 2].DeliveryDate.ToString();
                    j++;

                }
                app.DisplayAlerts = false;
              app.SaveWorkspace();
           
                app.Quit();
            
            }

            catch
            {
               
            }

            return RedirectToAction("Index", "AllPOsList");
        }

        //
        // GET: /Excel/Details/5

        public ViewResult Details(int id)
        {
            PO po = db.POs.Find(id);
            return View(po);
        }

        //
        // GET: /Excel/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Excel/Create

        [HttpPost]
        public ActionResult Create(PO po)
        {
            if (ModelState.IsValid)
            {
                db.POs.Add(po);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(po);
        }
        
        //
        // GET: /Excel/Edit/5
 
        public ActionResult Edit(int id)
        {
            PO po = db.POs.Find(id);
            return View(po);
        }

        //
        // POST: /Excel/Edit/5

        [HttpPost]
        public ActionResult Edit(PO po)
        {
            if (ModelState.IsValid)
            {
                db.Entry(po).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(po);
        }

        //
        // GET: /Excel/Delete/5
 
        public ActionResult Delete(int id)
        {
            PO po = db.POs.Find(id);
            return View(po);
        }

        //
        // POST: /Excel/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            PO po = db.POs.Find(id);
            db.POs.Remove(po);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }

     */ 
}